input_file = r'./data/BICS5-TLC_PE_DR-CT.dat'

# 直接计算某个文件的时间差值；
# 如果在最开始有delay，给出delay的时间，并算上没有delay的时间是多少

import re
from datetime import datetime, timedelta

def parse_timestamp(line):
    pattern = r'\[(\d{8}-\d{6})\]'
    match = re.search(pattern, line)
    if match:
        timestamp_str = match.group(1)
        timestamp = datetime.strptime(timestamp_str, '%Y%m%d-%H%M%S')
        return timestamp
    else:
        return None

def calculate_time_difference(file_path):
    with open(file_path, 'r') as file:
        lines = file.readlines()

    first_timestamp = parse_timestamp(lines[0])
    last_timestamp = parse_timestamp(lines[-1])

    if 'delay' in lines[0]:
        delay_timestamp = parse_timestamp(lines[1])
        delay_time = delay_timestamp - first_timestamp
        time_difference = last_timestamp - delay_timestamp
    else:
        delay_time = timedelta()
        time_difference = last_timestamp - first_timestamp

    return delay_time, time_difference


delay_time, time_difference = calculate_time_difference(input_file)
print(f'Delay time: {delay_time}')
print(f'Time difference: {time_difference}')
